<!doctype html>
<title>A visible but unfocused selection should receive ClipboardEvent</title>
<link rel="help" href="https://www.w3.org/TR/clipboard-apis/#fire-a-clipboard-event">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>

<input type="checkbox" id="checkbox">
<p contenteditable id="ce">HEY</p>

<script>
resetSelection = function(selectedElement) {
  selectedElement.innerHTML = "SOMETEXT"
  let range = document.createRange();
  range.selectNodeContents(selectedElement);
  window.getSelection().removeAllRanges();
  window.getSelection().addRange(range);
}

// We use testRunner.execCommand() to test user-triggered (not scripted) clipboard events.
testClipboardEvent = function(eventName, selectedElement) {
  let selectionHitCount = 0;
  let documentHitCount = 0;
  let lastTarget;
  selectionLog = function(e) {
    selectionHitCount++;
    lastTarget = e.target;
  }
  documentLog = function(e) {
    documentHitCount++;
    lastTarget = e.target;
  }
  selectedElement.addEventListener(eventName, selectionLog);
  document.addEventListener(eventName, documentLog);

  selectedElement.focus();
  resetSelection(selectedElement);
  testRunner.execCommand(eventName);
  assert_equals(selectionHitCount, 1, `The selection should receive a ${eventName}-event.`)
  assert_equals(documentHitCount, 1, `The document should receive a bubbled ${eventName}-event.`)
  assert_equals(lastTarget, selectedElement, `The selection should be the ${eventName}-event's target.`)
  resetSelection(selectedElement);

  checkbox.focus();
  testRunner.execCommand(eventName);
  assert_equals(selectionHitCount, 2, `The selection should receive a ${eventName}-event.`)
  assert_equals(documentHitCount, 2, `The document should receive a bubbled ${eventName}-event.`)
  assert_equals(lastTarget, selectedElement, `The selection should be the ${eventName}-event's target.`)
  resetSelection(selectedElement);

  checkbox.blur();
  testRunner.execCommand(eventName);
  assert_equals(selectionHitCount, 3, `The selection should receive a ${eventName}-event.`)
  assert_equals(documentHitCount, 3, `The document should receive a bubbled ${eventName}-event.`)
  assert_equals(lastTarget, selectedElement, `The selection should be the ${eventName}-event's target.`)
  resetSelection(selectedElement);

  document.body.focus();
  testRunner.execCommand(eventName);
  assert_equals(selectionHitCount, 4, `The selection should receive a ${eventName}-event.`)
  assert_equals(documentHitCount, 4, `The document should receive a bubbled ${eventName}-event.`)
  assert_equals(lastTarget, selectedElement, `The selection should be the ${eventName}-event's target.`)
  resetSelection(selectedElement);
}

test(() => testClipboardEvent('copy', ce),  'Visible but unfocused selections are targeted by copy-events.');
test(() => testClipboardEvent('paste', ce), 'Visible but unfocused selections are targeted by paste-events.');
test(() => testClipboardEvent('cut', ce),   'Visible but unfocused selections are targeted by cut-events.');
</script>
